using UnityEngine;
using System.Collections.Generic;

namespace EntityKit.Runtime
{
    public class LocalizationConfig : ScriptableObject
    {
        [SerializeField]
        private List<LocalizationEntry> entries = new List<LocalizationEntry>();
        
        [System.NonSerialized]
        private Dictionary<string, string> runtimeLookup;
        
        [System.NonSerialized]
        private bool isInitialized = false;
        
        public void InitializeRuntimeLookup()
        {
            if (isInitialized) return;
            
            runtimeLookup = new Dictionary<string, string>();
            foreach (var entry in entries)
            {
                string key = $"{entry.Language}_{entry.Type}_{entry.Key}";
                runtimeLookup[key] = entry.Value;
            }
            isInitialized = true;
        }
        
        public bool TryGetValue(Language language, LocalizationType type, string key, out string value)
        {
            if (!isInitialized) InitializeRuntimeLookup();
            
            string lookupKey = $"{language}_{type}_{key}";
            return runtimeLookup.TryGetValue(lookupKey, out value);
        }
        
        public void AddEntry(Language language, LocalizationType type, string key, string value)
        {
            entries.Add(new LocalizationEntry(language, type, key, value));
        }
        
        public List<LocalizationEntry> GetAllEntries()
        {
            return new List<LocalizationEntry>(entries);
        }
        
        public void ClearEntries()
        {
            entries.Clear();
            runtimeLookup?.Clear();
            isInitialized = false;
        }
    }
    
    [System.Serializable]
    public struct LocalizationEntry
    {
        public Language Language;
        public LocalizationType Type;
        public string Key;
        public string Value;
        
        public LocalizationEntry(Language language, LocalizationType type, string key, string value)
        {
            Language = language;
            Type = type;
            Key = key;
            Value = value;
        }
    }
    
    public enum LocalizationType
    {
        Text,
        Image,
        Audio,
        Font,
        Video,
        Prefab,
        ScriptableObject,
        TextAsset
    }
}